package masterjava.client.gxt.highcharts;

import org.adapters.highcharts.codegen.sections.options.OptionPath;
import org.adapters.highcharts.codegen.types.SeriesType;
import org.adapters.highcharts.codegen.utils.ClientConsole;
import org.adapters.highcharts.gxt.widgets.HighChart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * User: gkislin
 * Date: 7/19/11
 */
public class HighchartsFactory {

    public final static String TYPES[] = new String[]{
            "spline",
            "column",
            "areaspline",
            "area",
            "line"
    };

    public static HighChart getRawChart() {
        final HighChart hc = new HighChart(null, "spline");
        List<Map<String, Object>> series =
                new Vector<Map<String, Object>>();

        Map<String, Object> series1 = new HashMap<String, Object>();
        series1.put("type", "area");
        series1.put("data", new int[]{4, 5, 2, 10});

        Map<String, Object> series2 = new HashMap<String, Object>();
        series2.put("type", "line");
        series2.put("data", new int[]{14, 53, 0, 7});

        series.add(series1);
        series.add(series2);

        try {
            hc.setOption(new OptionPath("/series"), series);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            hc.setOption(new OptionPath("/credits/text"),
                    "Sample of HighChart-GXT");
            hc.setOption(new OptionPath("/credits/href"),
                    "http://sourceforge.net/projects/highcharts-gxt/");
        } catch (Exception e) {
        }
        return hc;
    }

    public static HighChart getChart(int num) {
        String type = TYPES[num];
        final HighChart hc = new HighChart(null, type);
        try {
            hc.setOption(new OptionPath("/title/text"), type);
            hc.setOption(new OptionPath("/credits/enabled"), false);
            hc.setOption(new OptionPath("/xAxis/allowDecimals"), false);
            hc.setOption(new OptionPath("/xAxis/title/text"), "And the X axis");
            hc.setOption(new OptionPath("/yAxis/title/text"), "And the Y axis");
            hc.setOption(new OptionPath("/yAxis/min"), 0);
            hc.setOption(new OptionPath("/subtitle/text"), "the subtitle");

            hc.setOption(new OptionPath("/plotOptions/spline/marker/enabled"), true);
            hc.setOption(new OptionPath("/plotOptions/spline/marker/radius"), 4);
            hc.setOption(new OptionPath("/plotOptions/spline/marker/lineColor"), "#666666");
            hc.setOption(new OptionPath("/plotOptions/spline/marker/lineWidth"), 1);
        } catch (Exception e) {
            ClientConsole.err("Building options", e);
        }

        for (int i = 0; i < 5; i++) {
            SeriesType series = new SeriesType("line #" + (i + 1));
            series.setType(type);
            for (int j = 0; j < 10; j++) {
                series.addEntry(new SeriesType.SeriesDataEntry(
                        com.google.gwt.user.client.Random.nextInt(500)));
            }
            hc.addSeries(series);
        }

        // no offset in the resize
        hc.setHeightOffset(0);
        // reduces the refresh delay from 1000 to 100 it seems to work
        hc.setResizeDelay(100);

        hc.followWindowResize(false);
        return hc;
    }
}
